//引入端口号
import config from './config'

//暴露请求函数
export default function request(url, data, method = 'GET') {
  return new Promise((resolve, rejects) => {
    wx.request({
      url: config.host + url,
      method,
      data,
      header: {
        // 让所有的请求携带cookie
        cookie: wx.getStorageSync('cookie_key') && wx.getStorageSync('cookie_key').join(';')
      },
      success: (res) => {
        if (data && data.isLogin) {
          wx.setStorageSync('cookie_key', res.cookies)
        }
        resolve(res.data)
      },
      fail: (res) => {
        rejects(res)
      }
    })
  })
}